<template>
	<view class="home">
		<navbar :isSearch="true" v-model="value" @input="change"></navbar>
		<view class="home-list">
			<view class="label-box" v-if="is_history">
				<view class="label-header">
					<text class="label-title">搜索历史</text>
					<text class="label-clear" @click="clear">清空</text>
				</view>
				<view class="label-content" v-if="historyLists.length>0">
					<view class="label-content_item" v-for="item in historyLists" :key="item_id" @click="openHistory(item)">
						{{item.name}}
					</view>
				</view>
				<view class="no-data" v-else>
					没有搜索记录
				</view>
			</view>
			<list-scroll class="list-scroll" v-else>
				<uni-load-more v-if="loading" status="loading" iconType="snow"></uni-load-more>
				<view v-if="searchList.length>0">
					<list-card v-for="(item,index) in searchList" :key="index" :item="item" @click="setHistory">
					</list-card>
				</view>
				<view class="no-data" v-if="searchList.length === 0 && !loading">
					没有搜索到相关内容
				</view>
			</list-scroll>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				// historyLists:[],
				value: '',
				is_history: true,
				searchList: [],
				loading:false
			}
		},
		computed: {
			...mapState(['historyLists'])
		},
		methods: {
			// 搜索功能
			getSearch(value) {
				if (!value) {
					this.searchList = []
					this.is_history = true
					return
				}
				this.is_history = false
				this.loading = true
				this.$api.get_search({
					value
				}).then((res) => {
					let result = res.data
					this.is_history = false
					this.searchList = result
					this.loading = false
				}).catch((err)=>{
					this.loading = false
				})
			},
			// 输入搜索内容请求
			change(value) {
				// console.log(this.historyLists)
				this.value = value
				if (!value) {
					clearTimeout(this.timer)
					this.mark = false
					this.getSearch(value)
					return
				}
				// 定时请求
				if (!this.mark) {
					this.mark = true
					this.timer = setTimeout(() => {
						this.mark = false
						this.getSearch(value)
						// console.log(111)
					}, 300)
				}
				// console.log(value)
			},
			// 存储搜索记录
			setHistory() {
				this.$store.dispatch('set_history', {
					name: this.value
				})
			},
			//点击搜索记录 显示相关内容
			openHistory(item) {
				this.value = item.name
				this.getSearch(this.value)
			},
			clear(){
				this.$store.dispatch('clear_history')
				uni.showToast({
					title:'记录已清空'
				})
			}
			
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
		background-color: #f5f5f5;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;

		.label-box {
			background-color: #fff;
			margin-bottom: 10px;
		}

		.label-header {
			display: flex;
			justify-content: space-between;
			font-size: 16px;
			padding: 10px 15px;
			font-weight: 600;
			color: #666;

			// border-bottom: 1px #f5f5f5 solid;
			.label-title {
				color: $mk-base-color;
			}

			.label-clear {
				color: #30b33a;
			}
		}

		.label-content {
			display: flex;
			flex-wrap: wrap;
			padding: 15px;
			padding-top: 0;

			.label-content_item {
				padding: 2px 10px;
				margin-top: 12px;
				margin-right: 10px;
				border: 1px #666 solid;
				border-radius: 5px;
				font-size: 14px;
				color: #666;
			}
		}
	}

	.no-data {
		height: 200px;
		line-height: 200px;
		width: 100%;
		text-align: center;
		font-size: 12px;
		color: #666;
	}
</style>
